package com.lovo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.bean.SysCommodity;
import com.lovo.dao.ISysCommodityDao;

import com.lovo.dto.SysGoodsDto;
import com.lovo.dto.TenderInfoDto;
import com.lovo.entity.SysCommodityEntity;
import com.lovo.mapper.SysCommodityMapper;
import com.lovo.service.ISysCommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-09-17
 */
@Service
public class SysCommodityServiceImpl extends ServiceImpl<SysCommodityMapper, SysCommodity> implements ISysCommodityService {

    @Autowired
    SysCommodityMapper sysCommodityMapper;

    @Autowired
    ISysCommodityDao sysCommodityDao;


    @RabbitListener(queues = "GoodsQueue")
    public void sendProduct(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, SysGoodsDto sysGoodsDto) throws IOException {

        SysCommodity sysCommodity = sysCommodityMapper.selectById(sysGoodsDto.getGoodsid());
        if (sysCommodity == null) {
            sysCommodity = new SysCommodity();
            sysCommodity.setProductid(sysGoodsDto.getGoodsid());
            sysCommodity.setPCode(sysGoodsDto.getPCode());
            sysCommodity.setPPrice(sysGoodsDto.getSellprice());
            sysCommodity.setPBatch(sysGoodsDto.getPBatch());
            sysCommodity.setPType(sysGoodsDto.getPType());
            sysCommodity.setPName(sysGoodsDto.getGoodsname());
            sysCommodity.setPState("上架中");
            this.save(sysCommodity);

        } else {
            sysCommodity.setPState("上架中");
            this.getBaseMapper().updateById(sysCommodity);
        }


        //手动确认
        channel.basicAck(tag, false);

    }


    @Override
    public IPage<SysCommodity> findShelfProduct(Integer pageNo, String type) {
        QueryWrapper<SysCommodity> qw = new QueryWrapper<>();
        if (type != null && type.length() > 0) {
            qw.like("p_type", type);
        }
        qw.eq("p_state", "上架中");
        return sysCommodityMapper.selectPage(new Page<>(pageNo, 5), qw);
    }


    @Override
    public void listings(Integer sysCommodityId, Integer sellPrice, String imgPath, String info) {
        //找到上架中商品对象
        SysCommodity sysCommodity = sysCommodityMapper.selectById(sysCommodityId);
        SysCommodityEntity sysCommodityEntity = new SysCommodityEntity();
        sysCommodityEntity.setProductid(sysCommodity.getProductid());
        sysCommodityEntity.setPCode(sysCommodity.getPCode());
        sysCommodityEntity.setPName(sysCommodity.getPName());
        sysCommodityEntity.setPBatch(sysCommodity.getPBatch());
        sysCommodityEntity.setPType(sysCommodity.getPType());
        sysCommodityEntity.setPState("已上架");
        sysCommodityEntity.setSellPrice(sellPrice);
        sysCommodityEntity.setImgPath(imgPath);
        sysCommodityEntity.setInfo(info);

        //将entity对象放入es数据库
        sysCommodityDao.save(sysCommodityEntity);


        sysCommodity.setPPrice(sellPrice);
        sysCommodity.setPState("已上架");
        sysCommodityMapper.updateById(sysCommodity);
    }

    @Override
    public List<SysCommodityEntity> findAllCommodityEntityByInfo(String info) {
        if (info == null || info.length() == 0) {
            return sysCommodityDao.findAll();
        }

        return sysCommodityDao.findAllByInfo(info);
    }

    @Override
    public void downProduct(Integer productId) {

        SysCommodity sysCommodity = sysCommodityMapper.selectById(productId);
        if (sysCommodity != null) {
            sysCommodity.setPState("已下架");
            sysCommodityMapper.updateById(sysCommodity);
            sysCommodityDao.deleteByProductid(productId);
        }

    }


}
